package com.example.medicalzb.service;

import com.example.medicalzb.entity.CompanyPolicyEntity;
import com.example.medicalzb.mapper.CompanyPolicyMapper;
import com.example.medicalzb.model.CompanyPolicyModel;
import com.example.medicalzb.param.CompanyPolicyParam;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CompanyPolicyService {
    @Autowired
    private CompanyPolicyMapper companyPolicyMapper;
    public Msg getAllPolicyWithPage(CompanyPolicyParam companyPolicyParam) {
        if (companyPolicyParam.getPn() == null){
            companyPolicyParam.setPn(1);
        }
        if (companyPolicyParam.getSize() == null){
            companyPolicyParam.setSize(Integer.MAX_VALUE);
        }
        if (companyPolicyParam.getSize() == 0){
            companyPolicyParam.setSize(1);
        }
        PageHelper.startPage(companyPolicyParam.getPn(),companyPolicyParam.getSize());
        List<CompanyPolicyModel> list = companyPolicyMapper.getAllPolicy(companyPolicyParam);
        PageInfo<CompanyPolicyModel> pageInfo = new PageInfo<>(list,5);
        return Msg.success().data("policyInfo",pageInfo);

    }

    public Msg saveMedicalPolicy(CompanyPolicyParam companyPolicyParam) {
        companyPolicyParam.setCreateTime(new DateTime().toDate());
        companyPolicyParam.setUpdateTime(new DateTime().toDate());
        CompanyPolicyEntity companyPolicyEntity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(companyPolicyParam,companyPolicyEntity);
        int i = companyPolicyMapper.saveMedicalPolicy(companyPolicyEntity);
        List<CompanyPolicyModel> allPolicy = companyPolicyMapper.getAllPolicy(null);
        companyPolicyEntity.setTotal((long) allPolicy.size());
        if (i > 0){
            Long num = companyPolicyEntity.getTotal() % 5 == 0 ? (companyPolicyEntity.getTotal() / 5) : ((companyPolicyEntity.getTotal() / 5) + 1);
            return Msg.success().mess("添加公司政策成功").data("numberOfAdd",i).data("pages",num);
        }
        return Msg.fail().mess("添加公司政策失败");
    }

    public Msg updateMedicalPolicy(Long id, CompanyPolicyParam companyPolicyParam) {
        companyPolicyParam.setUpdateTime(new DateTime().toDate());
        companyPolicyParam.setId(id);
        CompanyPolicyEntity companyPolicyEntity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(companyPolicyParam,companyPolicyEntity);
        int i = companyPolicyMapper.updateMedicalPolicy(companyPolicyEntity);
        if (i > 0){
            return Msg.success().mess("修改公司政策成功").data("updateData",companyPolicyEntity);
        }
        return Msg.fail().mess("修改公司政策失败");
    }

    public Msg deleteMedicalPolicy(Long id) {
        int i = companyPolicyMapper.deleteMedicalPolicy(id);
        if (i > 0){
            return Msg.success().mess("删除公司政策成功").data("numberOfDelete",i);
        }
        return Msg.fail().mess("删除公司政策失败");
    }
}
